﻿Ext.define('app.prm.search.Substance', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    border: false,
    initComponent: function () {
        var me = this;
        me.items = [me.getSearchGrid(), me.getSearchForm()];
        me.callParent(arguments);
    },
    getSearchGrid: function () {
        if (!this.searchGrid) {
            this.store = Ext.create("app.ux.PageInquiryStore", { url: '/api/services/app/search/GetSubstance', autoLoad: false });
            this.pagingBar = Ext.create({
                xtype: 'pagingtoolbar',
                store: this.store,   // same store GridPanel is using
                dock: 'bottom',
                displayInfo: true
            });
            this.searchGrid = new Ext.grid.Panel({
                title: '化合物列表',
                viewConfig: {
                    enableTextSelection: true
                },
                border: false,
                multiColumnSort: true,
                forceFit: false,
                store: this.store,
                dockedItems: [this.pagingBar],
                region: 'center',
                columns: [{
                    text: "#", xtype: 'rownumberer', width: 50
                },
                    {
                        header: '结构式', width: 200, dataIndex: 'molPngBase64', renderer: function (value, p, record) {
                            if (value.length > 10) {
                                return '<img width="160" height="100" src="data:image/jpg;base64,' + value + '" />';
                            }
                            return value;
                        }
                    },
                    { header: 'Cas', dataIndex: 'cas' },
                    { header: '中文名', dataIndex: 'cnName' },
                    { header: '英文名', dataIndex: 'enName' },
                    { header: '分子式', dataIndex: 'formula' },
                    { header: '分子量', dataIndex: 'molecularWeight' },
                    { header: '高风险', dataIndex: 'highRisk' },
                    { header: '备注', dataIndex: 'note' }
                ]
            });
        }
        return this.searchGrid;
    },
    getSearchForm: function () {
        var me = this;
        if (!this.searchForm) {
            this.searchForm = new Ext.form.Panel({
                title: '查询条件',
                defaultType: 'textfield',
                border: false,
                split: true,
                region: 'east',
                width: 300,
                defaults: { anchor: '95%' },
                fieldDefaults: {
                    labelWidth: 70,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        xtype: 'textarea',
                        fieldLabel: 'Cas',
                        emptyText: '多个请用逗号或分号分割',
                        height: 70,
                        name: 'cas'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '中文名',
                        emptyText: '多个请用逗号或分号分割',
                        height: 70,
                        name: 'cnName'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '英文名',
                        emptyText: '多个请用逗号或分号分割',
                        height: 70,
                        name: 'enName'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '分子式',
                        emptyText: '多个请用逗号或分号分割',
                        height: 70,
                        name: 'formula'
                    },
                    {
                        xtype: 'panel',
                        border: false,
                        layout: {
                            type: 'hbox',
                            align: 'middle ',
                            pack: 'center'
                        },
                        items: [
                            {
                                xtype: 'button',
                                text: '查询',
                                width: 50,
                                handler: function () { me.search(); }
                            },
                            { border: false, width: 3 },
                            {
                                xtype: 'button',
                                text: '清空',
                                width: 50,
                                handler: function () { me.clearSearch(); }
                            }
                        ]
                    }
                ]
            });
        }
        return this.searchForm;
    },
    search: function () {
        var me = this;
        var valJson = me.getSearchForm().getForm().getValues();

        if (valJson.cas == ""
            && valJson.cnName == ""
            && valJson.enName == ""
            && valJson.formula == "") {
            Ext.Msg.alert('系统提示', '至少输入一个查询条件');
            return false;
        }
        me.getSearchGrid().store.filter = valJson;
        me.pagingBar.moveFirst();
    },
    clearSearch: function () {
        var me = this;
        me.getSearchForm().getForm().reset();
    }
});